/*
 * Copyright 2007 MBARI
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


package org.mbari.vcr;

//import javax.comm.CommPort;
//import javax.comm.CommPortIdentifier;
//import javax.comm.NoSuchPortException;
//import javax.comm.PortInUseException;
//import javax.comm.SerialPort;
//import javax.comm.UnsupportedCommOperationException;


/**
 * Class description
 *
 *
 * @version    $Id: $
 * @author     Brian Schlining    
 */
public class VCRTestJavaComm1 {

//  SerialPort serialPort;
//  private long receiveTimeout = 40;
//  InputStream in;
//  OutputStream out;
//  
//  public VCRTestJavaComm1(String serialPortName) throws NoSuchPortException, PortInUseException, UnsupportedCommOperationException, IOException {
//          init(serialPortName);
//  }
//  
//  public VCRTestJavaComm1(String serialPortName, long receiveTimeout) throws NoSuchPortException, PortInUseException, UnsupportedCommOperationException, IOException {
//          this.receiveTimeout = receiveTimeout;
//          init(serialPortName);
//  }
//
//  
//  void init(String serialPortName) throws NoSuchPortException, PortInUseException, UnsupportedCommOperationException, IOException {
//          CommPortIdentifier portId = CommPortIdentifier.getPortIdentifier(serialPortName);
//          serialPort = (SerialPort) portId.open("VCR", 2000);
//    serialPort.setSerialPortParams(
//            38400,
//            SerialPort.DATABITS_8,
//            SerialPort.STOPBITS_1,
//            SerialPort.PARITY_ODD);
//    serialPort.setFlowControlMode(
//            SerialPort.FLOWCONTROL_RTSCTS_IN
//            & SerialPort.FLOWCONTROL_RTSCTS_OUT);
//
//    ((CommPort) serialPort).enableReceiveTimeout(
//            (int) receiveTimeout);    // Returns after waiting 190ms
//    in = serialPort.getInputStream();
//    out = serialPort.getOutputStream();
//  }
//  
//  public void send(byte[] data) throws IOException {
//          byte checksum = VCRReply.calculateChecksum(data);
//          final byte[] c = new byte[data.length + 1];
//          System.arraycopy(data, 0, c, 0, data.length);
//          c[c.length - 1] = checksum;
//          System.out.println(">> " + NumberUtil.toHexString(c));
//          out.write(c);
//  }
//  
//  public byte[] receive() throws IOException {
//          // Get the command returned by the VCR
//    final byte[] cmd = new byte[2];
//    in.read(cmd);
//
//    // Extract the number of data bytes in the command block. Then
//    // read the data from the serial port
//    final int numDataBytes = (int) (cmd[0] & 0x0F);    // Get the number of data blocks
//    byte[] data = null;
//    if (numDataBytes > 0) {
//        data = new byte[numDataBytes];
//        in.read(data);
//    }
//
//    // Read the checksum that the VCR sends
//    final byte[] checksum = new byte[1];
//    in.read(checksum);
//    
//    /*
//     * Munge it all into a single byte array
//     */
//    int dataLength = data == null ? 0 : data.length;
//  final byte[] c = new byte[cmd.length + dataLength + 1];
//          System.arraycopy(cmd, 0, c, 0, cmd.length);
//          if (data != null) {
//                  System.arraycopy(data, 0, c, cmd.length, data.length);
//          }
//          c[c.length - 1] = checksum[0];
//          
//          System.out.println("<< " + NumberUtil.toHexString(c));
//          return c;
//  }
//  
//  
//  
//  public static void main(String[] args) {
//          int ok = 0;
//          try {
//                  byte[] response = null;
//                  VCRTestJavaComm1 test = new VCRTestJavaComm1("COM2");
//                  test.send(VCR.GET_STATUS);
//                  response = test.receive();
//                  test.send(VCR.GET_TIMECODE);
//                  response = test.receive();
//                  test.send(VCR.PLAY_FWD);
//                  response = test.receive();
//                  test.send(VCR.GET_STATUS);
//                  response = test.receive();
//                  test.send(VCR.GET_TIMECODE);
//                  response = test.receive();
//                  Thread.sleep(2000);
//                  test.send(VCR.STOP);
//                  response = test.receive();
//                  test.send(VCR.GET_TIMECODE);
//                  response = test.receive();
//                  Thread.sleep(2000);
//                  test.send(VCR.GET_TIMECODE1);
//                  response = test.receive();
//                  Thread.sleep(2000);
//                  test.send(VCR.GET_TIMECODE2);
//                  response = test.receive();
//                  Thread.sleep(2000);
//                  test.send(VCR.GET_STATUS);
//                  response = test.receive();
//                  Thread.sleep(2000);
//          } catch (Exception e) {
//                  // TODO Auto-generated catch block
//                  e.printStackTrace();
//                  ok = -1;
//          } 
//          
//          System.exit(ok);
//  }
}
